package tool

import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"image/color"
	"myserver/response"
)

type CaptchaResult struct {
	Id          string `json:"id"`
	Base64Blob  string `json:"base_64_blob"`
	VerityValue string `json:"code"`
}

// base64Captcha 创建一个验证码并返回
func GenerateCaptchaHandler(context *gin.Context) {
	parameters := base64Captcha.ConfigCharacter{
		Height:                 30,
		Width:                  60,
		Mode:                   3,
		IsUseSimpleFont:        false,
		ComplexOfNoiseText:     0,
		ComplexOfNoiseDot:      0,
		IsShowHollowLine:       false,
		IsShowNoiseDot:         false,
		IsShowNoiseText:        false,
		IsShowSlimeLine:        false,
		IsShowSineLine:         false,
		ChineseCharacterSource: "",
		SequencedCharacters:    nil,
		UseCJKFonts:            false,
		CaptchaLen:             4,
		BgHashColor:            "",
		BgColor: &color.RGBA{
			R: 3,
			G: 102,
			B: 214,
			A: 254,
		},
	}
	captchaId, captchaInstance := base64Captcha.GenerateCaptcha("", parameters)
	base64blob := base64Captcha.CaptchaWriteToBase64Encoding(captchaInstance)
	captchaResult := CaptchaResult{
		Id:         captchaId,
		Base64Blob: base64blob,
	}
	response.Success(context, map[string]interface{}{
		"captcha_result": captchaResult,
	})
}

// 判断验证码是否正确
func VerityCaptcha(id string, value string) bool {
	verityResult := base64Captcha.VerifyCaptcha(id, value)
	return verityResult
}
